package org.thunms.schedule.quartz.controller;

import java.io.IOException;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.thunms.framework.controller.ControllerDefault;
import org.thunms.framework.model.DataGrid;
import org.thunms.framework.model.DataGridJson;
import org.thunms.framework.model.Json;
import org.thunms.framework.utils.UtilDateTime;
import org.thunms.schedule.quartz.model.QrtzJobs;
import org.thunms.schedule.quartz.service.CronExpressionTest;
import org.thunms.schedule.quartz.service.QrtzJobsService;
/**
 * 工作任务引擎
 * 任务管理子系统
 * 平台任务列表
 */
@Controller
@RequestMapping("/schedule/quartz/qrtzJobs")
public class QrtzJobsController extends ControllerDefault {
	private static final long serialVersionUID = -7121227734608985385L;
	private static final Logger logger =LoggerFactory.getLogger(QrtzJobsController.class);

	public QrtzJobsController() {
		super("schedule", "quartz", "qrtzJobs");
		logger.debug("执行模块初始化"+this.getRerutnURL(""));
	}
	
	@Autowired
	private QrtzJobsService qrtzJobsService;

	
	@RequestMapping(value = "/dataGridList")
	@ResponseBody
	public DataGridJson<QrtzJobs> dataGridList(DataGrid dg, QrtzJobs mode) {
		return this.qrtzJobsService.getDataGridList(dg, mode);
	}
	@RequestMapping(value="/advancedAdd")
	public String advancedAdd(String jobsId,HttpServletRequest request, HttpServletResponse response){
		request.setAttribute("jobsId", jobsId);
		return this.getRerutnURL("advanced_add");
	}
	@RequestMapping(value="/periodAdd")
	public String periodAdd(){
		return this.getRerutnURL("period_add");
	}
	@RequestMapping(value="/simpleAdd")
	public String simpleAdd(){
		return this.getRerutnURL("simple_add");
	}
	/**
	 * 综合配置界面
	 * @return
	 */
	@RequestMapping(value="/timer")
	public String timer(){
		return this.getRerutnURL("timer");
	}
	
	/**
	 * 定时任务保存
	 * @return
	 */
	@RequestMapping(value="/timerSave")
	@ResponseBody
	public Json timerSave(String jobsId,String jobName,String jobGroup,String qrtzJobKey,Date qrtzstartTime,Date qrtzendTime,String qrtzType,Integer repeatCount,Integer repeatInterval,Integer val,String selType,String cronExpression,HttpServletRequest request, HttpServletResponse response){
		Json msg=new Json();
		
		if(qrtzJobsService.JobExist(jobName, jobGroup)){
			msg.setSuccess(false);
			msg.setMsg("调度已经存在,请修改任务名称！");
		}else{
			qrtzJobsService.getDataGridList(null, null);
			if("1".equals(qrtzType)){
				if(qrtzJobsService.simpleAddSave(jobsId,jobName,jobGroup,qrtzJobKey, qrtzstartTime, qrtzendTime, repeatCount, repeatInterval)){
					msg.setSuccess(true);
					msg.setMsg("调度添加成功！");
				}else{
					msg.setSuccess(false);
					msg.setMsg("调度添加失败！");
				}
			}else if("2".equals(qrtzType)){
				if(qrtzJobsService.periodAddSave(jobsId,jobName,jobGroup,qrtzJobKey, val, selType,qrtzstartTime,qrtzendTime)){
					msg.setSuccess(true);
					msg.setMsg("调度添加成功！");
				}else{
					msg.setSuccess(false);
					msg.setMsg("调度添加失败！");
				}
			}else if("3".equals(qrtzType)){
				if(qrtzJobsService.sdvancedAddSave(jobsId,jobName,jobGroup,qrtzJobKey, cronExpression,qrtzstartTime,qrtzendTime)){
					msg.setSuccess(true);
					msg.setMsg("调度添加成功！");
				}else{
					msg.setSuccess(false);
					msg.setMsg("调度添加失败！");
				}
			}
		}
		
		
		return msg;
	}
	
	/**
	 * 高级任务添加
	 * @return
	 */
	@RequestMapping(value="/sdvancedAddSave")
	@ResponseBody
	public Json sdvancedAddSave(String jobsId,String jobName,String jobGroup,String qrtzJobKey,String cronExpression,HttpServletRequest request, HttpServletResponse response){
		Json msg=new Json();
		msg.setSuccess(true);
		String startTime=request.getParameter("qrtzstartTime");
		String endTime=request.getParameter("qrtzendTime");
		Date startTimeDate=null;
		Date endTimeDate=null;
		if(StringUtils.isNotBlank(startTime)){
			startTimeDate=UtilDateTime.toStringDate(startTime, UtilDateTime.DATE_TIME_SHORT_FORMAT);
		}
		if(StringUtils.isNotBlank(endTime)){
			endTimeDate=UtilDateTime.toStringDate(endTime, UtilDateTime.DATE_TIME_SHORT_FORMAT);
		}
		qrtzJobsService.sdvancedAddSave(jobsId,jobName,jobGroup,qrtzJobKey, cronExpression,startTimeDate,endTimeDate);
		msg.setMsg("高级任务添加成功！");
		return msg;
	}
	/**
	 * 简单任务添加
	 * @return
	 */
	@RequestMapping(value="/simpleAddSave")
	@ResponseBody
	public Json simpleAddSave(String jobsId,String jobName,String jobGroup,String qrtzJobKey,HttpServletRequest request, HttpServletResponse response){
		Json msg=new Json();
		String startTime=request.getParameter("qrtzstartTime");
		String endTime=request.getParameter("qrtzendTime");
		Date startTimeDate=null;
		Date endTimeDate=null;
		if(StringUtils.isNotBlank(startTime)){
			startTimeDate=UtilDateTime.toStringDate(startTime, UtilDateTime.DATE_TIME_SHORT_FORMAT);
		}
		if(StringUtils.isNotBlank(endTime)){
			endTimeDate=UtilDateTime.toStringDate(endTime, UtilDateTime.DATE_TIME_SHORT_FORMAT);
		}
		String repeatCount=request.getParameter("repeatCount");
		String repeatInterval=request.getParameter("repeatInterval");
		int repeatCountIntInteger=0;
		int repeatIntervalInteger=0;
		if(StringUtils.isNotBlank(repeatCount)){
			repeatCountIntInteger=Integer.parseInt(repeatCount);
		}
		if(StringUtils.isNotBlank(repeatInterval)){
			repeatIntervalInteger=Integer.parseInt(repeatInterval);
		}
		qrtzJobsService.simpleAddSave(jobsId,jobName,jobGroup,qrtzJobKey, startTimeDate, endTimeDate, repeatCountIntInteger, repeatIntervalInteger);
		msg.setSuccess(true);
		msg.setMsg("简单任务添加成功！");
		return msg;
	}
	/**
	 * 周期任务添加
	 * @return
	 */
	@RequestMapping(value="/periodAddSave")
	@ResponseBody
	public Json periodAddSave(String jobsId,String jobName,String jobGroup,String qrtzJobKey,Integer val,String selType,HttpServletRequest request, HttpServletResponse response){
		Json msg=new Json();
		String startTime=request.getParameter("qrtzstartTime");
		String endTime=request.getParameter("qrtzendTime");
		Date startTimeDate=null;
		Date endTimeDate=null;
		if(StringUtils.isNotBlank(startTime)){
			startTimeDate=UtilDateTime.toStringDate(startTime, UtilDateTime.DATE_TIME_SHORT_FORMAT);
		}
		if(StringUtils.isNotBlank(endTime)){
			endTimeDate=UtilDateTime.toStringDate(endTime, UtilDateTime.DATE_TIME_SHORT_FORMAT);
		}
		qrtzJobsService.periodAddSave(jobsId,jobName,jobGroup,qrtzJobKey, val, selType,startTimeDate,endTimeDate);
		msg.setSuccess(true);
		msg.setMsg("周期任务添加成功！");
		return msg;
	}
	
	/**
	 * 测试表达式
	 * @throws IOException 
	 * 
	 */
	@RequestMapping(value = "/cronExpression")
	public void cronExpression(String cron,HttpServletRequest request, HttpServletResponse response) throws IOException {
		response.getWriter().print(CronExpressionTest.testCronExpression(cron));
	}

}
